# coding=utf8
'''
Created on 2013-5-28

@author: Soul
'''
from core.Configurator import Configurator
from core.Logger import *
from core.Assigner import *
from core.EngineReader import *
import os, os.path

if __name__ == '__main__':
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')

    config = Configurator('./env.cfg')
    Logger.get_instance().init()
    if config:
        rn = Configurator.get_val('row_number')
        urllist = []
        engine = EngineReader()
        key_word = None
        if len(sys.argv) > 1:
            key_word = sys.argv[1]
            key_word = key_word.decode("ISO-8859-1")
            urllist = engine.read_seeds('./engines.xml', key_word)

        if key_word == None:
            key_word = Configurator.get_val('key_word')
            if key_word == None:
                key_word_list_file = Configurator.get_val('key_word_list').strip()
                if os.path.exists(key_word_list_file):
                    ff = open(key_word_list_file, "r")
                    key_word_list = ff.readlines()
                    ff.close()

                    for kwd in key_word_list:
                        kwd = kwd.decode('UTF8')
                        urllist.extend(engine.read_seeds('./engines.xml', kwd))
            else:
                key_word = key_word.decode('UTF8')
                urllist = engine.read_seeds('./engines.xml', key_word)

        if len(urllist):
            feed(urllist)
            # feed([('http://www.allzhaopin.com/zhiwei/job10356736.html?utm_source=zhiwei&utm_medium=baidu&utm_campaign=ald', '全才招聘网'.decode('utf8').encode('gbk'), {})])
            # feed(['http://www.chinacar.com.cn/ggcx_new/search_json.asp'])
            # feed(['http://car.i-ev.com/'])
            start()